open Ast;;
open Lexfor;;
open Lexing;;
 
let subsName  (i , n) s=
  let p=String.length s in
  let debut=ref (-1) in
  let fin=ref (-1) in
    for j=0 to (p-1)
    do
      if s.[j]='['
      then debut:=j
      else if s.[j]=']'
      then fin:=j;
    done;
    if !debut>=0 && !fin >= !debut then begin
    let sous_mot=String.sub s (!debut+1) (!fin- !debut -1) in
    let n= (Gramfor.peace Lexfor.rfor (Lexing.from_string (sous_mot^"#"))) in
      (String.sub s 0 (!debut+1))^(string_of_int(n))^(String.sub s !fin (p- !fin))
    end
    else s;;
	
(*subsName("i",42) "54a[i]"="54a[42]"*)

let rec subsExpAst (i, n) exp =match exp with
  | Affectation(lString,lexp)->
      Affectation(List.map (subsName(i,n)) lString, List.map (subsExpAst(i,n)) lexp)
  | Variable(s)->Variable(subsName(i,n) s)
  | Appel(str,lexp)->
        Appel(subsName(i,n) str,List.map (subsExpAst(i,n)) lexp)
  |_->exp;;


let iter(d,f,i,s)=
  let l=ref [] in
  for k=f downto d
  do
    Hashtbl.add Compthash.compt_tab i k;
    l:=(List.map (subsExpAst(i,k)) s)@(!l)
  done;
!l;;
